/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listenerthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import static klassetvserver.ServerListener.clientArray;
import static klassetvserver.ServerListener.clientLobbyArray;

public class ServerListenerThreadIn implements Runnable {

    private final BufferedReader in;
    private final ServerListenerThread main;
    private Request req;

    //Constructor
    public ServerListenerThreadIn(BufferedReader in, ServerListenerThread main) throws IOException {
        this.in = in;
        this.main = main;


//        threadInListener();

    }

    public void run() {
        try {
            threadInListener();
        } catch (IOException ex) {
            Logger.getLogger(ServerListenerThreadIn.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    enum Request {

        getLobby, setOpponent, setMyCurrentImageAndGuessedLetters,
        sendChatMessage, requestGame, rejectGame, acceptGame, setWord, leaveLobby, currentStatusChange,
        disconnect, CLOSECONNECTION, ERROR
    }

    public void threadInListener() throws IOException {
        String line;

        //Receive the clientName
        String name = in.readLine();
        main.setClientName(name);
        updateLobby();

        while (true) {
            try {
                //read first input line from client, should be the REQUEST
                line = in.readLine();


                System.out.println(line);
                //convert the request to enmum Request
                try {
                    req = Request.valueOf(line);
                } catch (IllegalArgumentException ex) {
                    req = Request.ERROR;

                }

                System.out.println("RECEIVED REQUEST: " + req + " FROM CLIENT: " + main.getClientName());
                //execute the request
                switch (req) {
                    case getLobby:
                        main.getThreadOut().getLobby();

                        break;
                    case setOpponent: {
                        int id = Integer.valueOf(in.readLine());
                        main.setOpponentId(id);
                        main.setOpponent(clientArray[id]);
                        System.out.println("Opponent Name: " + main.getOpponent().getOpponent().getClientName());
                        break;
                    }

                    case setMyCurrentImageAndGuessedLetters:
                        int imageCount = Integer.valueOf(in.readLine());
                        System.out.println("ThreadIn received imageCount: " + imageCount);
                        String guessedLetters = in.readLine();
                        System.out.println("ThreadIn received guessedLetters: " + guessedLetters);

                        main.getOpponent().getThreadOut().setOpponentCurrentImageAndGuessedLetters(imageCount, guessedLetters);

                        break;
                    case sendChatMessage:
                        String message = in.readLine();
                        System.out.println("ThreadIn received chatMessage: " + message);

                        main.getOpponent().getThreadOut().sendMessage(message);

                        break;
                    case requestGame: {
                        int id = Integer.valueOf(in.readLine());
                        System.out.println("ThreadIn received requestGame to client: " + id);
                        clientArray[id].getThreadOut().requestGame(clientArray[main.getClientId()]);
                        main.setRequestedOpponentId(id);
                        break;
                    }

                    case acceptGame:
                        main.setOpponentId(main.getRequestedOpponentId());
                        main.setOpponent(main.getRequestOpponent());

                        main.getOpponent().getThreadOut().acceptGame();
                        break;
                    case rejectGame:
                        main.setRequestOpponent(null);

                        main.getRequestOpponent().getThreadOut().rejectGame();
                        break;
                    case setWord:
                        String word = in.readLine();
                        System.out.println("ThreadIn received setWord: " + word);

                        main.getOpponent().getThreadOut().setWord(word);
                        break;
                    case leaveLobby:
                        clientLobbyArray[main.getClientId()] = null;
                        updateLobby();
                        break;
                    case currentStatusChange:
                        int statusChange = Integer.valueOf(in.readLine());
                        System.out.println("ThreadIn received currentStatusChange: " + statusChange);
                        if(main.getOpponent()!=null){
                            main.getOpponent().getThreadOut().statusChange(statusChange);
                        }
                        
                        break;
                    case CLOSECONNECTION:
                        throw new IOException();
                    default:
                        System.out.println("ThreadIn WRONG REQUEST: " + line);
                        break;

                }
            } catch (IOException ex) {
                System.out.println("DISCONNECTED: " + main.getClient().getInetAddress().getHostName() + ", Name: " + main.getClientName());
                System.out.println("DEBUG: Closing streams");
                clientArray[main.getClientId()] = null;
                clientLobbyArray[main.getClientId()] = null;
                in.close();
                break;
            }
        }

    }

    private void updateLobby(){
        for (int i = 0; i < clientLobbyArray.length; i++) {
            if (clientLobbyArray[i] != null) {
                if (clientLobbyArray[i].getThreadOut() != null) {
                    clientLobbyArray[i].getThreadOut().updateLobby();
                }
            }
        }
    }
}
